package com.example.satu.quizcreator.quiz_creator;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.example.satu.quizcreator.quiz.Question;
import com.example.satu.quizcreator.R;
import com.example.satu.quizcreator.quiz_creator.interfaces.onQuestionListItemClickListener;

import roboguice.fragment.RoboListFragment;

/**
 * Created by Satu on 2014-07-24.
 */
public class QuestionListFragment extends RoboListFragment  {

    private QuestionAdapter questionAdapter;
    private onQuestionListItemClickListener itemClickListener;

    public QuestionAdapter getQuestionAdapter() {
        return questionAdapter;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        questionAdapter = new QuestionAdapter(getActivity(), R.layout.item_question);
        setListAdapter(questionAdapter);
        initializeList();
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
            if(itemClickListener != null)
                if(position == 0)
                    itemClickListener.onListItemClick(null);
                else
                    itemClickListener.onListItemClick(questionAdapter.getItem(position));

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
       // Toast.makeText(getActivity(),getParentFragment().getClass().getName(),Toast.LENGTH_LONG).show();
       if(!(getParentFragment() instanceof onQuestionListItemClickListener)){
            throw new RuntimeException("Twoja activity nie implementuje onNewsitemClickListneer");
        }
        itemClickListener = (onQuestionListItemClickListener) getParentFragment();
    }

    private void initializeList(){
            Question question = new Question();
            question.setId(0);
            questionAdapter.add(question);

    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

}
